﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain;
using Nop.Core.Domain.Advert;
using Nop.Core.Domain.Localization;
using Nop.Services.Localization;
using Nop.Services.Advert;
using Nop.Web.Extensions;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Security;
using Nop.Web.Framework.UI.Captcha;
using Nop.Web.Infrastructure.Cache;
using System.Text.RegularExpressions;

namespace Nop.Web.Controllers
{
    public class AdvertController : BaseNopController
    {

        #region Fields

        private readonly IAdvertisementService _advertisementService;

        #endregion

        #region Constructors
        public AdvertController(IAdvertisementService advertisementService)
        {
            this._advertisementService = advertisementService;
        }
        #endregion
        //
        // GET: /Advert/

        public ActionResult Top_Advert()
        {

                //获取可用图片广告
                var adv = _advertisementService.GetAdvertisement(4,(int)AdvertisementType.ImageAdvert,(int)AdvertisementPosition.TopBar,(int)AdvertisementLevel.HomePage, false);
                // 定义正则表达式用来匹配 img 标签    
                Regex regImg = new Regex(@"(?is)<img[^>]*>|((?!<img).)+", RegexOptions.IgnoreCase);
                
                foreach (var m in adv)
                {
                    // 搜索匹配的字符串    s
                    MatchCollection matches = regImg.Matches(m.AdvertContent);
                    foreach (Match img in matches)
                    {
                        if (img.Length > 10 && img.Value.Substring(0, 4).ToLower() == "<img")
                        {
                            m.AdvertContent = img.Value;
                            break;
                        }
                    }
                }
            return View(adv);
        }

        /// <summary>
        /// 每日广告
        /// </summary>
        /// <returns></returns>
        public ActionResult Daily_Specials()
        {
            var adv = _advertisementService.GetAdvertisement(1,(int)AdvertisementType.ImageAdvert,(int)AdvertisementPosition.RightRange,(int)AdvertisementLevel.HomePage, false).FirstOrDefault();
            // 定义正则表达式用来匹配 img 标签    
            Regex regImg = new Regex(@"(?is)<img[^>]*>|((?!<img).)+", RegexOptions.IgnoreCase);

            if (adv != null)
            {
                // 搜索匹配的字符串    s
                MatchCollection matches = regImg.Matches(adv.AdvertContent);
                foreach (Match img in matches)
                {
                    if (img.Length > 10 && img.Value.Substring(0, 4).ToLower() == "<img")
                    {
                        adv.AdvertContent = img.Value;
                        break;
                    }
                }
            }
            if (adv == null) adv = new Advertisement();
            return View(adv);
        }
    }
}
